import weCropper from '../../dist/weCropper.js'

const __network_image__ = 'http://image.smartisanos.cn/resource/a9ea11be5ffa8225110782fe3ac46a74.png'
const device = wx.getSystemInfoSync()
Page({
  data: {
  	cropperOpt: {
			id: 'cropper',
			width: device.windowWidth,
			height: device.windowWidth,
			scale: 2.5
		}
	},
  touchStart (e) {
    this.wecropper.touchStart(e)
  },
  touchMove (e) {
    this.wecropper.touchMove(e)
  },
  touchEnd (e) {
    this.wecropper.touchEnd(e)
  },
  getCropperImage () {
    this.wecropper.getCropperImage((src) => {
      if (src) {
        wx.previewImage({
          current: '', // 当前显示图片的http链接
          urls: [src] // 需要预览的图片http链接列表
        })
      } else {
        console.log('获取图片地址失败，请稍后重试')
      }
    })
  },
	uploadTap () {
  	this.wecropper.pushOrign(__network_image__)
	},
  onLoad (option) {
		const { cropperOpt } = this.data

		new weCropper(cropperOpt)
			.on('ready', (ctx) => {
				console.log(`wecropper is ready for work!`)
			})
			.on('beforeImageLoad', (ctx) => {
				console.log(`weCropper is ready`)
				console.log(`current canvas context:`, ctx)
				wx.showToast({
					title: '上传中',
					icon: 'loading',
					duration: 20000
				})
			})
			.on('imageLoad', (ctx) => {
				console.log(`picture loaded`)
				console.log(`current canvas context:`, ctx)
				wx.hideToast()
			})
  }
})
